﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace Buy2Site.Handlers
{
    /// <summary>
    /// Summary description for HandlerAddProductImageToWebSite
    /// </summary>
    public class HandlerAddProductImageToWebSite : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string productID = context.Request["productID"];
            int picutreID = Convert.ToInt32(context.Request["pictureNumerID"]);
            int id;
            bool pBool = int.TryParse(productID,out id);
            if (pBool)
            {
                using (Buy2SiteDBEntities ent = new Buy2SiteDBEntities())
                {
                    Product product = ent.Product.FirstOrDefault(x => x.ID == id);
                    switch (picutreID)
                    {
                        case 1:
                            context.Response.OutputStream.Write(product.Picture1, 0, product.Picture1.Length);                        
                            // supporting jpeg for now
                            //context.Response.ContentType = "Image/jpeg";
                            break;
                        case 2:
                            context.Response.OutputStream.Write(product.Picture2, 0, product.Picture2.Length);
                            // supporting jpeg for now
                            //context.Response.ContentType = "Image/jpeg";
                            break;
                        case 3:
                            context.Response.OutputStream.Write(product.Picture3, 0, product.Picture3.Length);
                            // supporting jpeg for now
                            //context.Response.ContentType = "Image/jpeg";
                            break;
                        default:
                            break;
                    }
                    // supporting jpeg for now
                    context.Response.ContentType = "Image/jpeg";
                    //if (product != null && product.Picture1 != null && picutreID==1)
                    //{
                    //    context.Response.OutputStream.Write(product.Picture1, 0, product.Picture1.Length);
                        
                    //    // supporting jpeg for now
                    //    context.Response.ContentType = "Image/jpeg";
                    //}
                    //if (product != null && product.Picture2 != null && picutreID == 2)
                    //{
                    //    context.Response.OutputStream.Write(product.Picture2, 0, product.Picture2.Length);

                    //    // supporting jpeg for now
                    //    context.Response.ContentType = "Image/jpeg";
                    //}
                    //if (product != null && product.Picture3 != null && picutreID == 3)
                    //{
                    //    context.Response.OutputStream.Write(product.Picture3, 0, product.Picture3.Length);

                    //    // supporting jpeg for now
                    //    context.Response.ContentType = "Image/jpeg";
                    //}
                }
            }

            /*
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
            */
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}